% Author: Yang Ge, geyang_xjnu@sina.com
% 
% calculate the sparks number of the ith fire work 
% parameters
% fitness of current seeds generation
% i'th seed vector
function result = calculate_sparks(seeds_fitness, ith)
global coef_explosion_sparks;
global coef_explosion_a;
global coef_explosion_b;

total = 0;
seeds_amount = length(seeds_fitness);
worse_fitness = max(seeds_fitness);
worse2ith = (worse_fitness - seeds_fitness(ith) + eps);

for n = 1:seeds_amount
    total = total + (worse_fitness - seeds_fitness(n));
end

spark_numbers = coef_explosion_sparks * worse2ith / (total + eps);

am = coef_explosion_a * coef_explosion_sparks;
bm = coef_explosion_b * coef_explosion_sparks;

if spark_numbers < am
    spark_numbers = round(am);
elseif spark_numbers > bm
    spark_numbers = round(bm);
else
    spark_numbers = round(spark_numbers);
end
result = spark_numbers;
end